/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.handlers;


import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.isDefaultName;

import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionUse;

import com.cea.papyrus.diagram.sequence.sedi.edit.IOEditPart;
import com.cea.papyrus.diagram.sequence.sedi.model.command.DelegatingCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.DirectEditCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.FindInteractionCommand;

/**
 * Handles renaming of Interaction Uses.
 * <p>
 * This handler is used from InteractionUseDirectEditPolicy.
 * </p>
 * 
 * @author frankd
 */
public class InteractionUseRenameHandler {
    public final EditPolicy hostPolicy;

    public InteractionUseRenameHandler(final EditPolicy hostPolicy) {
        this.hostPolicy = hostPolicy;
    }

    public Command getRenameCommand(final DirectEditRequest request) {
        final IOEditPart host = (IOEditPart) hostPolicy.getHost();
        final InteractionUse iuse = host.getUmlElement();
        final String refersToName = (String) request.getCellEditor().getValue();

        // Assertion should hold because IU isn't direct-editable without it
        assert iuse.getRefersTo() != null;

        final CompoundCommand cmd = new CompoundCommand();
        cmd.setLabel("Edit Interaction Use");

        final FindInteractionCommand findInteractionCmd = new FindInteractionCommand();
        findInteractionCmd.setName(refersToName);
        findInteractionCmd.setStart(iuse.getModel());

        final DelegatingCommand actionCommand = new DelegatingCommand();

        final Command glue1 = new Command() {
            public @Override
            void execute() {
                if (findInteractionCmd.getInteraction() == null) {
                    final DirectEditCommand renameCmd = new DirectEditCommand();
                    renameCmd.setNamedElement(iuse.getRefersTo());
                    renameCmd.setName(refersToName);
                    actionCommand.setCommand(renameCmd);
                } else {
                    final String oldRefersToName = iuse.getRefersTo().getName();

                    CompoundCommand updateCmd = new CompoundCommand();
                    updateCmd.add(new Command() {
                        private Interaction oldRefTo;

                        public @Override
                        void execute() {
                            oldRefTo = iuse.getRefersTo();
                            iuse.setRefersTo(findInteractionCmd.getInteraction());
                        }

                        public @Override
                        void undo() {
                            iuse.setRefersTo(oldRefTo);
                            oldRefTo = null;
                        }

                        public @Override
                        void dispose() {
                            oldRefTo = null;
                        }
                    });

                    if (isDefaultName(oldRefersToName, "Interaction")) {
//                        DeleteInteractionCommand delCmd = new DeleteInteractionCommand();
//                        delCmd.setInteraction(iuse.getRefersTo());
//                        updateCmd.add(delCmd);
                    }
                    actionCommand.setCommand(updateCmd);
                }
            }
        };

        cmd.add(findInteractionCmd);
        cmd.add(glue1);
        cmd.add(actionCommand);
        return cmd;
    }
}
